/*
 * Copyright 2011 The Rusted Roof, LLC.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.proteusframework.core.base;

import org.proteusframework.core.api.IDelegateConsumer;
import org.proteusframework.core.api.IPlatformDelegate;
import org.proteusframework.core.util.Assert;

public abstract class AbstractDelegateConsumer implements IDelegateConsumer
{
    /**
     * Injected platform delegate used to access platform services, including locating and instantiating other
     * Proteus objects.
     */
    protected IPlatformDelegate delegate = null;

    /**
     * Class that represents the extended capabilities that are available via the delegate after a cast.
     */
    protected Class<? extends IPlatformDelegate> delegateInterface;

    /**
     * {@inheritDoc}
     */
    @Override
    public final <T extends IPlatformDelegate> void setPlatformDelegate(final IPlatformDelegate delegate,
                                                                        Class<T> delegateInterface)
    {
        Assert.unitialized(this.delegate, "Application platform delegate is implicitly final and cannot be modified.");

        Assert.parameterNotNull(delegate, "Parameter 'delegate' must not be null");

        this.delegate = delegate;
        this.delegateInterface = delegateInterface;
        onDelegateInjected();
    }

    /**
     * Empty delegate injection event handler for descendant classes.
     */
    protected void onDelegateInjected()
    {

    }
}
